package com.company.笔试.两个栈实现队列;

import java.util.Stack;

/**
 * @ClassName StackAndQueue
 * @Description TODO
 * @Author 11
 * @Date 2021/5/23 21:43
 * @Version 1.0
 */
public class StackAndQueue {
    public static void main(String[] args) {
        MyQueue<Integer> myQueue = new MyQueue<>();
        //push into queue
        myQueue.push(1);
        myQueue.push(2);
        myQueue.push(3);

        //test pop
        System.out.println("队列出队的第" + "1" + "个是：" + myQueue.pop());
        System.out.println("队列出队的第" + "2" + "个是：" + myQueue.pop());
        System.out.println("队列出队的第" + "3" + "个是：" + myQueue.pop());
    }

}
class MyQueue<ElementType> {
    public MyQueue() {
    }
    private Stack<ElementType> stack1 = new Stack<>();
    private Stack<ElementType> stack2 = new Stack<>();
    //push : push to the first stack;
    public void push(ElementType e) {
        stack1.push(e);
    }
    //pop: first pop all the elements to the 2nd stack, then pop from the 2nd stack;
    public ElementType pop() {
        //from stack1 to stack2
        while (!stack1.empty()) {
            stack2.push(stack1.pop());
        }
        //pop out from stack2
        return stack2.pop();
    }
    public boolean isEmpty() {
        return stack1.empty();
    }
}
